package leetcode.秋招.华为OD;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/9/19 22:32
 * @description：
 */

public class Main2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        TreeNode root = null;
        Map<Integer, TreeNode> map = new HashMap<>();
        for (int i = 0; i < N; i++) {
            int son = in.nextInt();
            int father = in.nextInt();
            TreeNode snode = map.getOrDefault(son, new TreeNode(son));
            if (father == 0) {
                map.put(son, snode);
                root = snode;
                continue;
            }
            TreeNode fnode;
            fnode = map.getOrDefault(father, new TreeNode(father));
            if (fnode.left == null) {
                fnode.left = snode;
            } else {
                fnode.right = snode;
            }
            snode.parent = fnode;
            map.put(father, fnode);
            map.put(son, snode);
        }
        in.nextLine();
        String next = in.next();
        String[] delIdStrs = next.split(" ");
        for (String delIdStr : delIdStrs) {
            map.remove(Integer.parseInt(delIdStr));
        }
        if (!map.containsKey(root.val)) {
            System.out.println();
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        List<Integer> res = new ArrayList<>();
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode cur = queue.poll();
                if (map.containsKey(cur.val)) {
                    res.add(cur.val);
                    if (cur.left != null) {
                        queue.add(cur.left);
                    }
                    if (cur.right != null) {
                        queue.add(cur.right);
                    }
                }
            }
        }
        res.sort(Integer::compareTo);
        System.out.println(res);

    }
}

class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode parent;

    public TreeNode(int val) {
        this.val = val;
    }
}
